package com.zycfc.zsf.boot.util.http;

import org.apache.http.impl.conn.*;
import java.util.concurrent.*;
import java.security.cert.*;
import java.security.*;
import org.apache.http.conn.ssl.*;
import org.apache.http.config.*;
import org.apache.http.conn.socket.*;
import javax.net.ssl.*;
import org.apache.http.client.*;
import org.apache.http.conn.*;
import org.apache.http.message.*;
import java.util.*;
import org.apache.http.impl.client.*;
import org.apache.http.client.config.*;
import org.apache.http.impl.*;
import org.apache.http.*;

public class DefaultHttpClientBuilder
{
    private static PoolingHttpClientConnectionManager connectionManager() throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
        final PoolingHttpClientConnectionManager pollingConnectionManager = new PoolingHttpClientConnectionManager((Registry)socketFactoryRegistry(), (HttpConnectionFactory)null, (SchemePortResolver)null, (DnsResolver)null, 30L, TimeUnit.SECONDS);
        pollingConnectionManager.setMaxTotal((int)HttpClientProperties.MAX_TOTAL);
        pollingConnectionManager.setDefaultMaxPerRoute((int)HttpClientProperties.MAX_PER_ROUTE);
        return pollingConnectionManager;
    }
    
    private static SSLContext sslContext() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
        return new SSLContextBuilder().loadTrustMaterial((KeyStore)null, (TrustStrategy)new TrustStrategy() {
            public boolean isTrusted(final X509Certificate[] chain, final String authType) {
                return true;
            }
        }).build();
    }
    
    private static Registry<ConnectionSocketFactory> socketFactoryRegistry() throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
        final HostnameVerifier hostnameVerifier = (HostnameVerifier)NoopHostnameVerifier.INSTANCE;
        final SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext(), hostnameVerifier);
        return (Registry<ConnectionSocketFactory>)RegistryBuilder.create().register("http", (Object)PlainConnectionSocketFactory.getSocketFactory()).register("https", (Object)sslSocketFactory).build();
    }
    
    private static HttpClientBuilder httpClientBuilder() throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
        final HttpClientBuilder httpClientBuilder = HttpClients.custom();
        httpClientBuilder.setConnectionManager((HttpClientConnectionManager)connectionManager());
        httpClientBuilder.setRetryHandler((HttpRequestRetryHandler)new DefaultHttpRequestRetryHandler((int)HttpClientProperties.RETRY_COUNT, (boolean)HttpClientProperties.REQUEST_SENT_RETRY_ENABLED));
        httpClientBuilder.setKeepAliveStrategy((ConnectionKeepAliveStrategy)DefaultConnectionKeepAliveStrategy.INSTANCE);
        final List<Header> headers = new ArrayList<Header>();
        headers.add((Header)new BasicHeader("Accept-Encoding", HttpClientProperties.ACCEPT_ENCODING));
        headers.add((Header)new BasicHeader("Accept-Language", HttpClientProperties.ACCEPT_LANGUAGE));
        headers.add((Header)new BasicHeader("Connection", HttpClientProperties.CONNECTION));
        httpClientBuilder.setDefaultHeaders((Collection)headers);
        httpClientBuilder.setConnectionTimeToLive((long)HttpClientProperties.TIME_TO_LIVE, TimeUnit.SECONDS);
        httpClientBuilder.evictIdleConnections(30L, TimeUnit.SECONDS);
        httpClientBuilder.evictExpiredConnections();
        return httpClientBuilder;
    }
    
    public static CloseableHttpClient httpClient() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
        final RequestConfig.Builder customReqConf = RequestConfig.custom();
        customReqConf.setConnectTimeout((int)HttpClientProperties.CONNECT_TIMEOUT);
        customReqConf.setSocketTimeout((int)HttpClientProperties.READ_TIMEOUT);
        customReqConf.setConnectionRequestTimeout((int)HttpClientProperties.CONNECTION_REQUEST_TIMEOUT);
        return httpClientBuilder().setSSLContext(sslContext()).setConnectionManager((HttpClientConnectionManager)connectionManager()).setDefaultRequestConfig(customReqConf.build()).build();
    }
    
    public static CloseableHttpClient getNoReuseHttpClient() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
        final RequestConfig.Builder customReqConf = RequestConfig.custom();
        customReqConf.setConnectTimeout((int)HttpClientProperties.CONNECT_TIMEOUT);
        customReqConf.setSocketTimeout((int)HttpClientProperties.READ_TIMEOUT);
        customReqConf.setConnectionRequestTimeout((int)HttpClientProperties.CONNECTION_REQUEST_TIMEOUT);
        return httpClientBuilder().setConnectionReuseStrategy((ConnectionReuseStrategy)NoConnectionReuseStrategy.INSTANCE).setSSLContext(sslContext()).setDefaultRequestConfig(customReqConf.build()).build();
    }
}
